﻿
//Author: 张亚涛
//createtime:2016-02-28 15:13:18
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "MemberAccount" 表的方法;
    /// </summary>
    public class MemberAccountBase
    {
        const string SQL_Select = "select [ID],[OpenID],[OpType],[Money],[Memo],[Date],[TradeID] from [MemberAccount] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回MemberAccount表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<MemberAccount> GetAllMemberAccount()
        {
            List<MemberAccount> memberAccountList = new List<MemberAccount>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                MemberAccount memberAccount = new MemberAccount(reader);
                memberAccountList.Add(memberAccount);
            }
            reader.Close();
            return memberAccountList;
        }
        /// <summary>
        /// 根据MemberAccount表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static MemberAccount GetMemberAccountById(long id)
        {
            MemberAccount memberAccount = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                memberAccount = new MemberAccount(reader);
            }
            reader.Close();
            return memberAccount;
        }
        /// <summary>
        /// 获得账户余额
        /// </summary>
        /// <param name="openID">微信ID</param>
        /// <returns></returns>       
        public static decimal GetBlance(string openID)
        {
            decimal res;
            string sql = "SELECT SUM(Money) AS bMoney FROM MemberAccount WHERE (OpenID = '" + openID + "')";
            return decimal.TryParse(SQLHelper.ExecuteScalar(sql, null), out res) ? res : 0;
        }
        /// <summary>
        /// 获得退款金额
        /// </summary>
        /// <param name="orderID">订单号</param>
        /// <returns></returns>       
        public static decimal GetRefund(string orderID)
        {
            decimal res;
            string sql = "SELECT Money FROM MemberAccount WHERE OpType='退款' AND (TradeID = '" + orderID + "')";
            return decimal.TryParse(SQLHelper.ExecuteScalar(sql, null), out res) ? res : 0;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据MemberAccount表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteMemberAccountById(long id)
        {
            string sql = "delete from [MemberAccount] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        /// <summary>
        /// 根据月份删除数据 
        /// </summary>
        /// <param name="month">月份</param>
        /// <returns>返回受影响的行数</returns>
        public static int DeleteByMonth(string month)
        {
            string sql = "delete from [MemberAccount] where [Memo] = @Memo";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Memo",month)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(MemberAccount memberAccount)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",memberAccount.Id),
            new SqlParameter("@OpenID",memberAccount.OpenID),
            new SqlParameter("@OpType",memberAccount.OpType),
            new SqlParameter("@Money",memberAccount.Money),
            new SqlParameter("@Memo",memberAccount.Memo),
            new SqlParameter("@Date",memberAccount.Date),
            new SqlParameter("@TradeID",memberAccount.TradeID)    
            };
            return paras;
        }
        /// <summary>
        /// 根据MemberAccount表的ID字段更新数据 
        /// </summary> 
        /// <param name="MemberAccount">memberAccount</param>
        /// <returns></returns>       
        public static int UpdateMemberAccountById(MemberAccount memberAccount)
        {
            string sql = "update [MemberAccount] set [OpenID] = @OpenID,[OpType] = @OpType,[Money] = @Money,[Memo] = @Memo,[Date] = @Date,[TradeID] = @TradeID where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(memberAccount);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向MemberAccount表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="memberAccount">MemberAccount</param>       
        /// <returns></returns>        
        public static int AddMemberAccount(MemberAccount memberAccount)
        {
            string sql = "insert into [MemberAccount]([OpenID],[OpType],[Money],[Memo],[Date],[TradeID])values(@OpenID,@OpType,@Money,@Memo,@Date,@TradeID)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(memberAccount);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 向MemberAccount表插入一条数据（SQL语句）
        /// </summary>
        /// <param name="m">MemberAccount</param>       
        /// <returns></returns>        
        public static string AddMemberAccount_SQL(MemberAccount m)
        {
            string sql = "insert into [MemberAccount]([OpenID],[OpType],[Money],[Memo],[Date],[TradeID])values('" + m.OpenID + "','" + m.OpType + "','" + m.Money + "','" + m.Memo + "','" + m.Date + "','" + m.TradeID + "');";
            return sql;
        }
        /// <summary>
        /// 返回MemberAccount表的所有数据（SQL语句） 
        /// </summary>
        /// <param name="openID">微信ID</param>
        /// <returns></returns>        
        public static string GetAllMemberAccount_SQL(string openID)
        {
            string strSQL = SQL_Select + " where (OpenID = '" + openID + "')";
            return strSQL;
        }
        #endregion
    }
}